#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade

reload(sys)
sys.setdefaultencoding('utf8')

class gui_window:
    def all_text(self):
        self.iter_end = self.show_textview_buf.get_end_iter()
        self.iter_start = self.show_textview_buf.get_start_iter()
        self.show_textview_buf.delete(self.iter_start, self.iter_end)
        if self.fg_color == 8 and self.bg_color == 8:
            self.tag = self.show_textview_buf.create_tag()
        elif self.bg_color == 8 and self.fg_color != 8:
            self.tag = self.show_textview_buf.create_tag(foreground=self.color_combobox_dic[self.fg_color])
        elif self.bg_color != 8 and self.fg_color == 8:
            self.tag = self.show_textview_buf.create_tag(background=self.color_combobox_dic[self.bg_color])
        else:
            self.tag = self.show_textview_buf.create_tag(foreground=self.color_combobox_dic[self.fg_color],
                                                            background=self.color_combobox_dic[self.bg_color])

        self.input_text = self.input_entry.get_text()
        self.show_textview_buf.insert_with_tags(self.iter_end, self.input_text, self.tag)


    def on_input_entry_changed(self, widget):
        self.all_text()

    def on_combobox_changed(self, widget):
        self.input_text += self.combobox_dic[self.combobox.get_active()]
        self.input_entry.set_text(self.input_text)

    def on_fg_combobox_changed(self, widget):
        self.fg_color = self.fg_combobox.get_active()
        self.all_text()

    def on_bg_combobox_changed(self, widget):
        self.bg_color = self.bg_combobox.get_active()
        self.all_text()

    def on_add_prompt_button_clicked(self, widget):
        if self.input_entry.get_text() != '':
            self.prompt_text += '\\[\\e[%d;%dm\\]%s' % (30+self.fg_color, 40+self.bg_color,self.input_text)
            self.output_textview_buf.set_text(self.prompt_text+'\\[\\e[0m\\]'+'\\[\\e]2;'+self.title_text+'\\a\\]')
            self.fg_combobox.set_active(8)
            self.bg_combobox.set_active(8)
            self.combobox.set_active(16)
            self.input_entry.set_text('')
            self.show_textview_buf.set_text('')
            self.input_text = ''
            self.show_text = ''

    def on_add_title_button_clicked(self, widget):
        if self.input_entry.get_text() != '':
            self.fg_combobox.set_active(8)
            self.bg_combobox.set_active(8)
            self.combobox.set_active(16)
            self.title_text += '%s' % (self.input_text)
            self.output_textview_buf.set_text(self.prompt_text+'\\[\\e[0m\\]'+'\\[\\e]2;'+self.title_text+'\\a\\]')
            self.input_entry.set_text('')
            self.show_textview_buf.set_text('')
            self.input_text = ''
            self.show_text = ''

    def on_clean_button_clicked(self, widget):
        self.fg_combobox.set_active(8)
        self.bg_combobox.set_active(8)
        self.combobox.set_active(16)
        self.input_entry.set_text('')
        self.show_textview_buf.set_text('')
        self.output_textview_buf.set_text('')
        self.input_text = ''
        self.show_text = ''
        self.real_text = ''
        self.title_text = ''
        self.prompt_text = ''
        self.fg_color = 8
        self.bg_color = 8


    def __init__(self):
        self.wTree = gtk.glade.XML('ui_PS1editor.glade')
        self.window = self.wTree.get_widget('window')
        self.show_textview =  self.wTree.get_widget('show_textview')
        self.output_textview =  self.wTree.get_widget('output_textview')
        self.combobox = self.wTree.get_widget('combobox')
        self.fg_combobox = self.wTree.get_widget('fg_combobox')
        self.bg_combobox = self.wTree.get_widget('bg_combobox')
        self.set_combobox = self.wTree.get_widget('set_combobox')
        self.input_entry = self.wTree.get_widget('input_entry')
        self.add_prompt_button = self.wTree.get_widget('add_prompt_button')
        self.add_title_button = self.wTree.get_widget('add_title_button')
        self.clean_button = self.wTree.get_widget('clean_button')

        self.show_textview_buf = gtk.TextBuffer()
        self.output_textview_buf = gtk.TextBuffer()
        self.show_textview.set_buffer(self.show_textview_buf)
        self.output_textview.set_buffer(self.output_textview_buf)
        self.iter_start = self.show_textview_buf.get_start_iter()
        self.iter_end = self.show_textview_buf.get_end_iter()
        self.input_text = ''
        self.show_text = ''
        self.real_text = ''
        self.title_text = ''
        self.prompt_text = ''
        self.fg_color = 8
        self.bg_color = 8

        self.combobox_dic = {0: '\\u',
                            1: '\\h',
                            2: '\\H',
                            3: '\\s',
                            4: '\\A',
                            5: '\\t',
                            6: '\\T',
                            7: '\\@',
                            8: '\\w',
                            9: '\\W',
                            10: '\\v',
                            11: '\\$',
                            12: '\\j',
                            13: '\\!',
                            14: '\\#',
                            15: '\\I',
                            16: ''}
        self.color_combobox_dic = {0:'#000000',
                                    1:'#FF0000',
                                    2:'#008000',
                                    3:'#FFFF00',
                                    4:'#0000FF',
                                    5:'#800080',
                                    6:'#00FFFF',
                                    7:'#FFFFFF',
                                    8: 0}
        self.set_combobox_dic = {0:0,
                                1:1,
                                2:4,
                                3:7,
                                4:8}
        self.window.connect('destroy', gtk.main_quit)
        self.combobox.connect('changed', self.on_combobox_changed)
        self.fg_combobox.connect('changed', self.on_fg_combobox_changed)
        self.bg_combobox.connect('changed', self.on_bg_combobox_changed)
        self.input_entry.connect('changed', self.on_input_entry_changed)
        self.add_prompt_button.connect('clicked', self.on_add_prompt_button_clicked)
        self.add_title_button.connect('clicked', self.on_add_title_button_clicked)
        self.clean_button.connect('clicked', self.on_clean_button_clicked)

        self.tag = self.show_textview_buf.create_tag()

        self.window.show()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    w = gui_window()
    w.main()
